const Sequelize = require("sequelize");
const Op = require("sequelize").Op;

// Option 1: Passing parameters separately
const sequelize = new Sequelize("mydata", "root", "", {
  host: "localhost",
  dialect: "mysql" /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */,
  define: {
    charset: "utf8mb4"
  }
});
sequelize.authenticate();

const User = sequelize.define("users", {
  uname: {
    type: Sequelize.STRING,
    allowNull: false,
    unique: true
  },
  password: {
    type: Sequelize.STRING,
    allowNull: false
  },
  role: {
    type: Sequelize.STRING,
    allowNull: false,
    defaultValue: "user"
  },
  status: {
    type: Sequelize.BOOLEAN,
    defaultValue: true
  }
});

// 虚拟文件 virtual file
const Vfile = sequelize.define("vfile", {
  name: {
    type: Sequelize.STRING,
    allowNull: false
  },
  isFolder: {
    type: Sequelize.BOOLEAN,
    defaultValue: false
  },
  parentId: {
    type: Sequelize.INTEGER,
    allowNull: true
  }
});

User.hasMany(Vfile);
Vfile.belongsTo(User);

const md5 = require("md5");

User.create({
  name: "admin",
  password: md5(md5("123")),
  role: "admin",
  status: 1
});

// 真实文件 real file
const Rfile = sequelize.define("rfile", {
  size: {
    type: Sequelize.INTEGER,
    allowNull: false
  },
  path: {
    type: Sequelize.STRING,
    allowNull: false,
    unique: true
  },
  type: {
    type: Sequelize.STRING
  }
});

Rfile.hasMany(Vfile);
Vfile.belongsTo(Rfile);

sequelize.sync();

module.exports = {
  User,
  Rfile,
  Vfile,
  Op,
  Sequelize
};

// 初始化数据库
// create database mydata
// use mydata
// insert into users (uname,password,role,status,createdAt,updatedAt) values('admin','d9b1d7db4cd6e70935368a1efb10e377','admin',1,NOW(),NOW());
// insert into users (uname,password,role,status,createdAt,updatedAt) values('我是谁','d9b1d7db4cd6e70935368a1efb10e377','admin',1,NOW(),NOW());
